<style type="text/css"></style>
<template>
    <i-form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="80">
        <Form-item label="姓名" prop="personalname">
            <i-input v-model="formValidate.personalname" placeholder="请输入姓名"></i-input>
        </Form-item>
        <Form-item label="证件类型" prop="papertype">
            <i-select v-mdel="formValidate.papertype" placeholder="请选择证件类型">
                <template v-for="(item,index) in papertypeArr">
                    <i-option :value="item.value">{{item.value}}</i-option>
                </template>
            </i-select>
        </Form-item>
        <Form-item label="证件号" prop="paperid">
            <i-input v-model="formValidate.paperid" placeholder="请输入证件号"></i-input>
        </Form-item>
        <Form-item label="性别" prop="sex">
            <i-select v-model="formValidate.sex" placeholder="请选择性别">
                <i-option value="男">男</i-option>
                <i-option value="女">女</i-option>
            </i-select>
        </Form-item>
        <!--<Form-item label="编队" prop="teamid">-->
            <!--<i-select v-model="formValidate.teamid" placeholder="编队">-->
                <!--<i-option value="1">一队</i-option>-->
                <!--<i-option value="2">二队</i-option>-->
                <!--<i-option value="3">三队</i-option>-->
            <!--</i-select>-->
        <!--</Form-item>-->
        <Form-item label="离开时间">
            <Row>
                <i-col span="11">
                    <Date-picker type="datetime" placeholder="离开部队时间" @on-change="changeOutDate"></Date-picker>
                </i-col>
            </Row>
        </Form-item>
        <Form-item label="状态" prop="state">
            <i-select v-model="formValidate.state" placeholder="请选择状态">
                <i-option value="正常">正常</i-option>
                <i-option value="退伍">退伍</i-option>
                <i-option value="休假">休假</i-option>
                <i-option value="禁用">禁用</i-option>
            </i-select>
        </Form-item>
        <Form-item>
            <i-button @click="handleSubmit('formValidate')" type="primary">提交</i-button>
            <i-button type="ghost" style="margin-left: 8px">取消</i-button>
        </Form-item>
    </i-form>

</template>
<script>
    import apiUtil from '../../assets/utils/api_util';

    export default {
        data() {
            return {
                formValidate: {
                    //添加字段
                    personalname: '',
                    paperid: '',
                    sex: '',
                    papertype: '',
                    // teamid: '',
                    outdate: '',
                    state: '',
                },
                ruleValidate: {
                    personalname: [
                        {required: true, message: '姓名不能为空', trigger: 'blur'}
                    ],
                    paperid: [
                        {required: true, message: '证件号不能为空', trigger: 'blur'},
                    ],
                    sex: [
                        {required: true, message: '请选择性别', trigger: 'change'}
                    ],
                    papertype: [
                        {required: true, message: '请选择证件类型', trigger: 'change'}
                    ],
                    // teamid: [
                    //     {required: true, message: '请选择编队', trigger: 'change'}
                    // ],
                    outdate: [
                        {required: true, type: 'date', message: '请选择离开部队时间', trigger: 'change'}
                    ],
                    state: [
                        {required: true, message: '请选择状态', trigger: 'change'}
                    ],
                },
                papertypeArr: [],
            }
        },
        mounted: function () {
            let id = this.$route.query.id;

            if (id && id.length > 0) {

                this.getDetail(id)
            }

        },

        methods: {
            changeOutDate: function (date) {
                console.log(date)
                this.formValidate.outdate = date;
            },
            handleSubmit(name) {

                let that = this;
                let model = this.$refs[name].model;
                this.$refs[name].validate((valid) => {
                    that.updateData(model);
                })
            },
            //获取证件类型
            getPaperData() {
                let that = this;

                let para = {};
                let params = that.$qs.stringify(para);

                this.$Loading.start();

                apiUtil.get(that, apiUtil.urls.paperTypeList, params, function (res) {
                    that.$Loading.finish();
                    that.papertypeArr = res.Msg;

                    console.log('res:', res)
                });

            },
            //详情
            getDetail: function (id) {
                let that = this;
                that.$Loading.start();

                let para = {
                    id: id,
                };


                apiUtil.get(that, apiUtil.urls.userDetail, para, function (res) {
                    that.$Loading.finish();
                    that.formValidate.personalname = res.Msg.personalname;
                    that.formValidate.sex = res.Msg.sex;
                    that.formValidate.paperid = res.Msg.paperid;
                    that.formValidate.papertype = res.Msg.papertype;
                    // that.formValidate.teamid = res.Msg.teamid;
                    that.formValidate.outdate = res.Msg.outdate;
                    that.formValidate.state = res.Msg.state;
                    that.getPaperData();
                    // that.formValidate.state =res.data.state.toString();
                });
            },
            //修改
            updateData: function (model) {
                let that = this;

                model.id = this.$route.query.id;

                let params = that.$qs.stringify(model);

                this.$Loading.start();

                apiUtil.post(that,apiUtil.urls.userEdit, params, function (res) {
                    that.$Loading.finish();
                    that.$message.success('编辑成功')
                });
            },


        }
    }
</script>
